package com.geaosu.kotlin.utils

import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.LayoutInflaterCompat


/**
 * 字体样式工具类
 */
class FontUtils {
    /**
     * 设置全局字体样式
     */
    fun setGlobalFontStyle(activity: AppCompatActivity, fontName: String) {
        if (fontName.isNotEmpty()) {
            val tf: Typeface = Typeface.createFromAsset(activity.assets, fontName)
            LayoutInflaterCompat.setFactory2(
                LayoutInflater.from(activity),
                object : LayoutInflater.Factory2 {
                    override fun onCreateView(
                        parent: View?,
                        name: String,
                        context: Context,
                        attrs: AttributeSet
                    ): View? {
                        val d: AppCompatDelegate = activity.delegate
                        val view: View = d.createView(parent, name, context, attrs)
                        if (view is TextView) {
                            view.typeface = tf
                        }
                        if (view is EditText) {
                            view.typeface = tf
                        }
                        return view
                    }

                    override fun onCreateView(
                        name: String,
                        context: Context,
                        attrs: AttributeSet
                    ): View? {
                        TODO("Not yet implemented")
                    }

                })
        }
    }
}